package the.broccoli.weixin.configuration;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.Serializable;

@Configuration
public class WxMpConfiguration implements Serializable {

    @Resource
    private WistWeiXinProperties wistWeiXinProperties;

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        configStorage.setAppId(wistWeiXinProperties.getMpAppid());
        configStorage.setSecret(wistWeiXinProperties.getMpSecret());
        configStorage.setToken(wistWeiXinProperties.getMpToken());
        configStorage.setAesKey(wistWeiXinProperties.getMpAesKey());
        return configStorage;
    }

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }
}
